package com.msb.mall.ware.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.msb.api.feign.product.ProductFeignApi;
import com.msb.common.vo.CommonPageReq;
import com.msb.common.vo.CommonResult;
import com.msb.mall.core.dto.SkuHasStockDto;
import com.msb.mall.core.entity.product.SkuInfoEntity;
import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.msb.common.utils.PageUtils;
import com.msb.common.utils.Query;

import com.msb.mall.core.dao.ware.WareSkuDao;
import com.msb.mall.core.entity.ware.WareSkuEntity;
import com.msb.mall.ware.service.WareSkuService;


@Service("wareSkuService")
public class WareSkuServiceImpl extends ServiceImpl<WareSkuDao, WareSkuEntity> implements WareSkuService {

    @Autowired
    private ProductFeignApi productFeignApi;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<WareSkuEntity> page = this.page(
                new Query<WareSkuEntity>().getPage(params),
                new QueryWrapper<WareSkuEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPageCondition(CommonPageReq<Map<String, Object>> req) {
        Page<WareSkuEntity> page = new Page<>(req.getCurrPage(), req.getPageSize());

        Map<String, Object> param = req.getData();
        LambdaQueryWrapper<WareSkuEntity> queryWrapper = new LambdaQueryWrapper<>();
        if (param != null) {
            String wareId = (String) param.get("wareId");
            queryWrapper.eq(StringUtils.isNotEmpty(wareId), WareSkuEntity::getWareId, wareId);

            String skuId = (String) param.get("skuId");
            queryWrapper.eq(StringUtils.isNotEmpty(skuId), WareSkuEntity::getSkuId, skuId);
        }
        Page<WareSkuEntity> wareSkuEntityPage = baseMapper.selectPage(page, queryWrapper);
        return new PageUtils(wareSkuEntityPage);
    }

    @Override
    public void addStock(Long skuId, Long wareId, Integer skuNum) {
        // 判断是否有改商品和仓库的入库记录
        LambdaQueryWrapper<WareSkuEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WareSkuEntity::getSkuId, skuId).eq(WareSkuEntity::getWareId, wareId);
        List<WareSkuEntity> list = baseMapper.selectList(queryWrapper);
        if(list == null || list.size() == 0){
            // 如果没有就新增商品库存记录
            WareSkuEntity entity = new WareSkuEntity();
            entity.setSkuId(skuId);
            entity.setWareId(wareId);
            entity.setStock(skuNum);
            entity.setStockLocked(0);
            try {
                // 动态的设置商品的名称
                CommonResult<SkuInfoEntity> commonResult = productFeignApi.info(skuId); // 通过Feign远程调用商品服务的接口
                if(commonResult.getCode() == HttpStatus.SC_OK){
                    SkuInfoEntity info = commonResult.getData();
                    entity.setSkuName(info.getSkuName());
                }
            }catch (Exception e){

            }
            baseMapper.insert(entity); // 插入商品库存记录
        }else{
            // 如果有就更新库存
            baseMapper.updateStock(skuId,wareId,skuNum);
        }
    }

    @Override
    public List<SkuHasStockDto> getSkusHasStock(List<Long> skuIds) {
        List<SkuHasStockDto> list = skuIds.stream().map(skuId -> {
            Long count = baseMapper.getSkuStock(skuId);
            SkuHasStockDto dto = new SkuHasStockDto();
            dto.setSkuId(skuId);
            dto.setHasStock(count > 0);
            return dto;
        }).collect(Collectors.toList());
        return list;
    }

}
